import { defineStore } from 'pinia';
import request from '@/api/index';
import { LocationQueryValue } from 'vue-router';
import { xmloading, getHMMSS, getYMD } from '@/utils/public';
import { getItem } from '@/utils/storage'
export const userStore = defineStore('user', {
  state: () => ({
    activeStatus:0,
    user_id:getItem('user_id') as LocationQueryValue | LocationQueryValue[],
    room_id:getItem('room_id') as LocationQueryValue | LocationQueryValue[],
    u_openid:getItem('u_openid') as LocationQueryValue | LocationQueryValue[],
    custom_id:getItem('custom_id') as LocationQueryValue | LocationQueryValue[],
    oss_url:'',
    /** 主界面菜单type */
    pageType:getItem('pageType') || 1,
    /** 主界面菜单 */
    tabsArr:[] as Array<any>,
    /**榜单类型type  1=魅力榜 2=人气榜 3=公会榜 4=房间榜 5=富豪榜 */
    rankingType:0,
    /**榜单类型type id 接口需要 */
    rankingID:0, 
    /**榜单类型菜单 */
    rankingTypeArr:[] as Array<any>,
    /**活动开始时间 */
    finalsTimeSt:0,
    /**活动结束时间 */
    finalsTimeEt:0,
    /**人气票 */
    ticket:0,
    /**金豆 */
    jd_balance:0,
    /**钻石 */
    diamond_balance:0,
    /**人气票活动列表 */
    banner_list:[] as Array<any>,
    /**榜单礼物列表 */
    giftList:[] as Array<any>,
    /**榜单日期列表 */
    rankingDayArr:[] as Array<any>,
    /**用户昵称 */
    nickName:'',
    /**用户头像 */
    portrait:'',
    /**规则列表 */
    ruleList:[] as Array<any>,
    /**留言板发送所需钻石金额 */
    lyb_diamand_num:0,
    /**留言板快捷留言内容 */
    lyb_content:[] as Array<any>,
    /**盖楼礼物 */
    foolGiftList:[] as Array<any>,
  }),
  getters: {
    
  },
  actions: {
    async getWallet() {
      const res = await request.getUserWallet();
      const { diamond_balance=0, jd_balance=0} = res.data;
      this.diamond_balance = diamond_balance
      this.jd_balance = jd_balance
    },
    async getSystemInfo() {
      const res = await request.getSystemInfo()
      const { now_time=0 } = res.data
      if(now_time < this.finalsTimeSt) {
        this.activeStatus = 1; // 活动未开始
      }
      if(now_time > this.finalsTimeEt) {
        this.activeStatus = 2; // 活动已结束
      }
    },
    getIntegerTimeNum (time:number = 0) {
      
    },
    async getfoolGiftList() {
      const res = await request.bridgeGift()
      const { data } = res
      this.foolGiftList = data
    },
    async getActInfo() {
      const loading = xmloading()
      const res = await request.gameInfo()
      loading.close()
      const {
        banner_list=[], css='',
        finalsTimeSt='', finalsTimeEt='',
        if_rank=0, if_build=0, if_lyb=0, if_recharge=0, 
        image='', name='', diamond_balance=0, jd_balance=0,
        rankConfig={}, custom_rule={}, oss_url='',lybConfig={}
      } = res?.data
      const { giftList=[], rankList=[] } = rankConfig; // 榜单配置
      const { rule='' } = custom_rule == null ? { rule:'' } : custom_rule; // 活动规则
      const { content=[], diamand_num=0 } = lybConfig; // 留言板配置
      this.oss_url = oss_url
      this.lyb_content = content
      this.lyb_diamand_num = diamand_num
      if(rule) {
        this.ruleList = JSON.parse(rule)
      } else {
        this.ruleList = []
      }
      this.finalsTimeSt = finalsTimeSt; // 开始时间
      this.finalsTimeEt = finalsTimeEt; // 结束时间
      this.jd_balance =  jd_balance; // 金豆
      this.diamond_balance = diamond_balance // 钻石
      // this.ticket = ticket // 人气票
      this.banner_list = banner_list; // 人气票参与活动banner
      this.portrait = image; // 用户头像
      this.nickName = name; // 用户昵称
      this.giftList = giftList; // 榜单礼物列表
      // 榜单日期列表
      const dayTiem = 60 * 60 * 24
      const rankingDayArr = [
        getHMMSS(finalsTimeSt * 1000)
      ]
      const dayKey = Number(((getYMD(finalsTimeEt * 1000) - getYMD(finalsTimeSt * 1000)) /dayTiem).toFixed(0)) - 1 || 0
      for(let i=1; i<=dayKey; i++) {
        rankingDayArr.push(getHMMSS((getYMD(finalsTimeSt * 1000) + (dayTiem * i)) * 1000))
      }
      rankingDayArr.push(getHMMSS(finalsTimeEt * 1000))
      this.rankingDayArr = rankingDayArr
      // 主界面菜单判断
      const tabsArr = []
      // 榜单处理
      if(if_rank == 1) {
        tabsArr.push({key:1,image:'pageTab1.png',notImage:'pageTabNot1.png'})
        const newRankingTypeArr = [] as Array<any>
        // 榜单类型菜单处理
        const rankingTypeArr = [
          {key:1,title:'魅力榜',image:'listTabImg1.png',notImage:'listTabNot1.png'},
          {key:2,title:'人气榜',image:'listTabImg2.png',notImage:'listTabNot2.png'},
          {key:5,title:'富豪榜',image:'listTabImg5.png',notImage:'listTabNot5.png'},
          {key:4,title:'房间榜',image:'listTabImg4.png',notImage:'listTabNot4.png'},
          {key:3,title:'公会榜',image:'listTabImg3.png',notImage:'listTabNot3.png'},
        ]
        rankingTypeArr.forEach((res,index)=>{
          const obj = rankList.find((item:any)=>item.rank_type == res.key) || { rank_type:0 }
          if(obj.rank_type) {
            newRankingTypeArr.push({
              ...res,
              ...obj,
            })
          }
        })
        const { key=0, rank_id=0 } = newRankingTypeArr[0] || { key:0, rank_id:0 }
        this.rankingType = key
        this.rankingID = rank_id
        this.rankingTypeArr = newRankingTypeArr
      }
      // 盖楼
      if(if_build == 1) {
        this.getfoolGiftList()
        tabsArr.push({key:2,image:'pageTab2.png',notImage:'pageTabNot2.png'})
      }
      // 留言板
      if(if_lyb == 1) tabsArr.push({key:3,image:'pageTab3.png',notImage:'pageTabNot3.png'})
      // 充值返利
      if(if_recharge == 1) tabsArr.push({key:4,image:'pageTab4.png',notImage:'pageTabNot4.png'})
      if(tabsArr.length > 0) {
        let newPageType = tabsArr.find(res=>res.key == this.pageType)?.key || tabsArr[0].key
        this.pageType = newPageType || 0
      }
      this.tabsArr = tabsArr
      return {
        css
      }
    }
  },
})